//
//  GGRouteRequest.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/2.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "GGRouteRequest.h"
#import "NSString+GGQuery.h"
@implementation GGRouteRequest

-(id)copyWithZone:(NSZone *)zone{
    GGRouteRequest *copyRequest = [[GGRouteRequest alloc] initWithURL:self.URL];
    copyRequest.routeParameters = self.routeParameters;
    copyRequest.routeExpression = self.routeExpression;
    copyRequest.primitiveParams = self.primitiveParams;
    copyRequest.jumpMethod = self.jumpMethod;
    copyRequest.isShowNav = self.isShowNav;
    copyRequest.navTitle = self.navTitle;
    copyRequest.isNavAnimation = self.isNavAnimation;
    copyRequest.targetCallBack = self.targetCallBack;
    copyRequest.isConsumed = self.isConsumed;
    self.isConsumed = YES;
    return copyRequest;
}

-(instancetype)initWithURL:(NSURL *)URL{
    if (!URL) {
        return nil;
    }
    self = [super init];
    if (self) {
        _URL = URL;
        _queryParameters = [[_URL query] GGParametersFromQueryString];
    }
    return self;
}
-(instancetype)initWithURL:(NSURL *)URL routeExpression:(NSString *)routeExpression routePatameters:(NSDictionary *)routePatameters primitivePatameters:(NSDictionary *)primitiveParameters isJumpMethod:(JumpMethod)jumpMethod  isShowNav:(BOOL)isShowNav NAVTitle:(NSString *)navTitle isNavAnimation:(BOOL)isNavAnimation targetCallBack:(void (^)(NSError *, id))targetCallBack{
    if (!URL) {
        return nil;
    }
    self = [super init];
    if (self) {
        _URL = URL;
        //将参数UTF8解码后存入字典
        _queryParameters = [[_URL query] GGParametersFromQueryString];
        _routeExpression = routeExpression;
        _routeParameters = routePatameters;
        _primitiveParams = primitiveParameters;
        _jumpMethod = jumpMethod;
        _isShowNav = isShowNav;
        _navTitle = navTitle;
        _isNavAnimation = isNavAnimation;
        self.targetCallBack = targetCallBack;
    }
    return self;
}




-(void)defaultFinishTargetCallBack{
    if (self.targetCallBack && self.isConsumed == NO) {
        self.targetCallBack(nil,@"正常执行回调");
    }
}
-(id)objectForKeyedSubscript:(NSString *)key{
    id value = self.routeParameters[key];
    if (!value) {
        value = self.queryParameters[key];
    }
    if (!value) {
        value = self.primitiveParams[key];
    }
    return value;
}

-(NSString *)description{
    return [NSString stringWithFormat:
            @"\n<%@ %p\n"
            @"\t URL: \"%@\"\n"
            @"\t queryParameters: \"%@\"\n"
            @"\t routeParameters: \"%@\"\n"
            @"\t PrimitiveParam: \"%@\"\n"
            @">",
            NSStringFromClass([self class]),
            self,
            [self.URL description],
            self.queryParameters,
            self.routeParameters,
            self.primitiveParams
            ];
}
-(NSURL *)callbackURL{
    if (!_callbackURL) {
        for (NSString *key in self.routeParameters.allKeys){
            if ([[key lowercaseString] rangeOfString:@"callback"].location != NSNotFound){
                NSString *urlstring = self.routeParameters[key];
                NSURL *url = [NSURL URLWithString:urlstring];
                if (url) {
                    _callbackURL = url;
                    return _callbackURL;
                }
            }
        }
        for (NSString *key in self.queryParameters.allKeys){
            if ([[key lowercaseString] rangeOfString:@"callback"].location != NSNotFound){
                NSString *urlstring = self.queryParameters[key];
                NSURL *url = [NSURL URLWithString:urlstring];
                if (url) {
                    _callbackURL = url;
                    return _callbackURL;
                }
            }
        }
        for (NSString *key in self.primitiveParams.allKeys){
            if ([[key lowercaseString] rangeOfString:@"callback"].location != NSNotFound){
                NSString *urlstring = self.primitiveParams[key];
                NSURL *url = [NSURL URLWithString:urlstring];
                if (url) {
                    _callbackURL = url;
                    return _callbackURL;
                }
            }
        }
        
    }
    return _callbackURL;
}

-(void)setTargetCallBack:(void (^)(NSError *, id))targetCallBack{
    __weak GGRouteRequest *weakRequest = self;
    if (targetCallBack == nil) {
        return;
    }
    self.isConsumed = NO;
    _targetCallBack = ^(NSError *error,id responseObject){
        weakRequest.isConsumed = YES;
        targetCallBack(error,responseObject);
    };
}
-(void)setRouteParameters:(NSDictionary *)routeParameters{
    _routeParameters = routeParameters;
}
-(void)setPrimitiveParams:(NSDictionary *)primitiveParams{
    _primitiveParams = primitiveParams;
}
-(void)setJumpMethod:(JumpMethod)jumpMethod{
    _jumpMethod = jumpMethod;
}
-(void)setIsShowNav:(BOOL)isShowNav{
    _isShowNav = isShowNav;
}
-(void)setIsNavAnimation:(BOOL)isNavAnimation{
    _isNavAnimation = isNavAnimation;
}
-(void)setNavTitle:(NSString *)navTitle{
    _navTitle = navTitle;
}
@end
